package com.woniuxy.usercoachassemble.configuration;

import com.woniuxy.usercoachassemble.entity.Adress;
import com.woniuxy.usercoachassemble.service.AdressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.BoundGeoOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class AdressConfiguration {
    @Autowired
    private AdressService adressServiceImpl;
    @Bean
    public ApplicationRunner cacheActiveAppRunner(RedisTemplate<String, String> redisTemplate) {

        return args -> {
            final String GEO_KEY = "address";
            redisTemplate.delete(GEO_KEY);
            System.out.println("初始化");
            List<Adress> lis=adressServiceImpl.query().list();
            Map<String, Point> points = new HashMap<>();
            for (Adress adress:lis){
//                获取经纬度
                Point point=new Point(adress.getLongtitude(),adress.getLatitude());
                points.put(adress.getUserId()+"",point);
                BoundGeoOperations<String, String> geoOps = redisTemplate.boundGeoOps(GEO_KEY);
                geoOps.add(points);
            }



        };
    }


}
